# -*- coding: utf-8 -*-
'''
@Software: PyCharm
@File    : search_douyin.py
@Author  : Bryan SHEN
@E-mail  : m18801919240_3@163.com
@Site    : Shanghai, China
@Time    : 2021-08-05
@Description:
'''

from functools import reduce
import pandas as pd
from DataSearchRematchByRe.utils.elastic_search import ESPostDou, ESLogShopDou, ESUserDou, ESStarDou
import re


class SeachDataDY(object):

    def __init__(self):
        pass

    def exclude_word_match(self, nwords_string):

        not_clauses = []
        if nwords_string != "":
            if "/" in nwords_string:
                word = nwords_string.split("/")
                not_clause1 = [{"match_phrase": {"description": i}} for i in word]
                not_clause2 = [{"match_phrase": {"promotions.title.text": i}} for i in word]
                not_clause3 = [{"match_phrase": {"ocr.text": i}} for i in word]
                not_clause4 = [{"match_phrase": {"anchor_info.title": i}} for i in word]
            else:
                word = nwords_string.split("￥")
                not_clause1 = [{"match_phrase": {"description": i}} for i in word]
                not_clause2 = [{"match_phrase": {"promotions.title.text": i}} for i in word]
                not_clause3 = [{"match_phrase": {"ocr.text": i}} for i in word]
                not_clause4 = [{"match_phrase": {"anchor_info.title": i}} for i in word]
            not_clauses = [not_clause1, not_clause2, not_clause3, not_clause4]

        run_function = lambda x, y: x if y in x else x + [y]
        not_clauses = reduce(run_function, [[], ] + not_clauses)
        return not_clauses

    def must_word_match(self, mwords_string):

        if "/" in mwords_string:
            words = mwords_string.split("/")
            should_clause1 = [{"bool": {"must": [{"match_phrase": {"description": w}} for w in words]}}]
            should_clause2 = [{"bool": {"must": [{"match_phrase": {"promotions.title.text": w}} for w in words]}}]
            should_clause3 = [{"bool": {"must": [{"match_phrase": {"ocr.text": w}} for w in words]}}]
            should_clause4 = [{"bool": {"must": [{"match_phrase": {"anchor_info.title": w}} for w in words]}}]
            # should_clause_boolmust = [should_clause1, should_clause2, should_clause3, should_clause4]
        elif "|" in mwords_string:
            words = mwords_string.split("|")
            should_clause1 = [{"match_phrase": {"description": w}} for w in words]
            should_clause2 = [{"match_phrase": {"promotions.title.text": w}} for w in words]
            should_clause3 = [{"match_phrase": {"ocr.text": w}} for w in words]
            should_clause4 = [{"match_phrase": {"anchor_info.title": w}} for w in words]
        else:
            words = mwords_string.split("￥")
            should_clause1 = [{"match_phrase": {"description": w}} for w in words]
            should_clause2 = [{"match_phrase": {"promotions.title.text": w}} for w in words]
            should_clause3 = [{"match_phrase": {"ocr.text": w}} for w in words]
            should_clause4 = [{"match_phrase": {"anchor_info.title": w}} for w in words]

        should_clauses = [should_clause1, should_clause2, should_clause3, should_clause4]
        run_function = lambda x, y: x if y in x else x + [y]
        should_clauses = reduce(run_function, [[], ] + should_clauses)
        return should_clauses, words

    def query_data(self, k, mwords_string, words, should_clause, time, not_clause):
        """ 查询逻辑 """

        count = []
        query = {
            # 筛选字段：若要在导出的结果中添加或删除字段，在这里操作
            "_source": ["user_id", "post_id", "comment_count", "share_count", "liked_count", "post_create_time",
                "play_url", "description", "ocr", "is_star_task", "promotions", "duration", "star_atlas_info.link_info",
                "play_count", "anchor_info.title"],
            "query": {
                "bool": {
                    "should": should_clause,
                    "minimum_should_match": 1,
                    "must": [
                        {
                            "range": {
                                "post_create_time": {
                                    "gte": time[0],
                                    "lte": time[1]
                                }
                            }
                        }
                    ],
                    "must_not": not_clause
                }
            }
        }
        ep = ESPostDou.scan(query)
        for i in ep:
            i['_source']['sum_match_keyword'] = k
            i['_source']['match_keyword'] = mwords_string
            if 'promotions' in i['_source']:
                if len(i['_source']['promotions']):
                    if len([x for x in words if x.upper() in str(i['_source']['promotions']).upper()]):
                        for j in i['_source']['promotions']:
                            o = {}
                            o['sum_match_keyword'] = k
                            o['match_keyword'] = mwords_string
                            if i['_source'].get('post_id', ''):
                                o['post_id'] = i['_source']['post_id']
                            if i['_source'].get('user_id', ''):
                                o['user_id'] = str(i['_source']['user_id'])
                            if i['_source'].get('ocr', ''):
                                o['ocr'] = i['_source']['ocr']
                            if i['_source'].get('comment_count', ''):
                                o['comment_count'] = i['_source']['comment_count']
                            if i['_source'].get('share_count', ''):
                                o['share_count'] = i['_source']['share_count']
                            if i['_source'].get('liked_count', ''):
                                o['liked_count'] = i['_source']['liked_count']
                            if i['_source'].get('play_count', ''):
                                o['play_count'] = i['_source']['play_count']
                            if i['_source'].get('duration', ''):
                                o['duration'] = i['_source']['duration']
                            if i['_source'].get('post_create_time', ''):
                                o['post_create_time'] = i['_source']['post_create_time']
                            if i['_source'].get('play_url', ''):
                                o['play_url'] = i['_source']['play_url'].replace('/play/', '/playwm/')
                            if i['_source'].get('description', ''):
                                o['description'] = i['_source']['description']
                            if i['_source'].get('is_star_task', ''):
                                o['is_star_task'] = i['_source']['is_star_task']
                            if i['_source'].get('anchor_info', ''):
                                if i['_source']['anchor_info'].get('title', ''):
                                    o['anchor_info_title'] =i['_source']['anchor_info']['title']
                            if i['_source'].get('star_atlas_info', ''):
                                if i['_source']['star_atlas_info'].get('link_info', ''):
                                    if i['_source']['star_atlas_info']['link_info'].get('web_url', ''):
                                        o['star_atlas_info_web_url'] = \
                                            i['_source']['star_atlas_info']['link_info']['web_url']
                                if i['_source']['star_atlas_info'].get('link_info', ''):
                                    if i['_source']['star_atlas_info']['link_info'].get('title', ''):
                                        o['star_atlas_info_title'] = \
                                            i['_source']['star_atlas_info']['link_info']['title']
                            if j.get('promotion_id', ''):
                                o['promotion_id'] = str(j['promotion_id'])
                            if j.get('title', ''):
                                o['title'] = j['title']
                            if j.get('price', ''):
                                o['price'] = j['price'] / 100
                            if j.get('sales', ''):
                                o['sales'] = j['sales']
                            if j.get('url', ''):
                                o['商品链接'] = j['url']
                            if o not in count:
                                count.append(o)
                    else:
                        for j in i['_source']['promotions'][:1]:
                            o1 = {}
                            o1['sum_match_keyword'] = k
                            o1['match_keyword'] = mwords_string
                            if i['_source'].get('post_id', ''):
                                o1['post_id'] = i['_source']['post_id']
                            if i['_source'].get('user_id', ''):
                                o1['user_id'] = str(i['_source']['user_id'])
                            if i['_source'].get('ocr', ''):
                                o1['ocr'] = i['_source']['ocr']
                            if i['_source'].get('comment_count', ''):
                                o1['comment_count'] = i['_source']['comment_count']
                            if i['_source'].get('share_count', ''):
                                o1['share_count'] = i['_source']['share_count']
                            if i['_source'].get('liked_count', ''):
                                o1['liked_count'] = i['_source']['liked_count']
                            if i['_source'].get('play_count', ''):
                                o1['play_count'] = i['_source']['play_count']
                            if i['_source'].get('duration', ''):
                                o1['duration'] = i['_source']['duration']
                            if i['_source'].get('post_create_time', ''):
                                o1['post_create_time'] = i['_source']['post_create_time']
                            if i['_source'].get('play_url', ''):
                                o1['play_url'] = i['_source']['play_url'].replace('/play/', '/playwm/')
                            if i['_source'].get('description', ''):
                                o1['description'] = i['_source']['description']
                            if i['_source'].get('is_star_task', ''):
                                o1['is_star_task'] = i['_source']['is_star_task']
                            if i['_source'].get('anchor_info', ''):
                                if i['_source']['anchor_info'].get('title', ''):
                                    o1['anchor_info_title'] =i['_source']['anchor_info']['title']
                            if i['_source'].get('star_atlas_info', ''):
                                if i['_source']['star_atlas_info'].get('link_info', ''):
                                    if i['_source']['star_atlas_info']['link_info'].get('web_url', ''):
                                        o1['star_atlas_info_web_url'] = \
                                            i['_source']['star_atlas_info']['link_info']['web_url']
                                if i['_source']['star_atlas_info'].get('link_info', ''):
                                    if i['_source']['star_atlas_info']['link_info'].get('title', ''):
                                        o1['star_atlas_info_title'] = \
                                            i['_source']['star_atlas_info']['link_info']['title']
                            if j.get('promotion_id', ''):
                                o1['promotion_id'] = str(j['promotion_id'])
                            if j.get('title', ''):
                                o1['title'] = j['title']
                            if j.get('price', ''):
                                o1['price'] = j['price'] / 100
                            if j.get('sales', ''):
                                o1['sales'] = j['sales']
                            if j.get('url', ''):
                                o1['商品链接'] = j['url']
                            if o1 not in count:
                                count.append(o1)
                else:
                    if i['_source'].get('user_id', ''):
                        i['_source']['user_id'] = str(i['_source']['user_id'])
                    if i['_source'].get('post_id', ''):
                        i['_source']['post_id'] = str(i['_source']['post_id'])
                    if i['_source'].get('play_url', ''):
                        i['_source']['play_url'] = i['_source']['play_url'].replace('/play/', '/playwm/')
                    if i['_source'].get('anchor_info', ''):
                        if i['_source']['anchor_info'].get('title', ''):
                            i['_source']['anchor_info_title'] = i['_source']['anchor_info']['title']
                    if i['_source'].get('star_atlas_info', ''):
                        if i['_source']['star_atlas_info'].get('link_info', ''):
                            if i['_source']['star_atlas_info']['link_info'].get('web_url', ''):
                                i['_source']['star_atlas_info_web_url'] = \
                                    i['_source']['star_atlas_info']['link_info']['web_url']
                        if i['_source']['star_atlas_info'].get('link_info', ''):
                            if i['_source']['star_atlas_info']['link_info'].get('title', ''):
                                i['_source']['star_atlas_info_title'] = \
                                    i['_source']['star_atlas_info']['link_info']['title']
                        del i['_source']['star_atlas_info']
                    count.append(i['_source'])
            else:
                if i['_source'].get('user_id', ''):
                    i['_source']['user_id'] = str(i['_source']['user_id'])
                if i['_source'].get('post_id', ''):
                    i['_source']['post_id'] = str(i['_source']['post_id'])
                if i['_source'].get('play_url', ''):
                    i['_source']['play_url'] = i['_source']['play_url'].replace('/play/', '/playwm/')
                if i['_source'].get('anchor_info', ''):
                    if i['_source']['anchor_info'].get('title', ''):
                        i['_source']['anchor_info_title'] = i['_source']['anchor_info']['title']
                    del i['_source']['anchor_info']
                if i['_source'].get('star_atlas_info', ''):
                    if i['_source']['star_atlas_info'].get('link_info', ''):
                        if i['_source']['star_atlas_info']['link_info'].get('web_url', ''):
                            i['_source']['star_atlas_info_web_url'] = \
                                i['_source']['star_atlas_info']['link_info']['web_url']
                    if i['_source']['star_atlas_info'].get('link_info', ''):
                        if i['_source']['star_atlas_info']['link_info'].get('title', ''):
                            i['_source']['star_atlas_info_title'] = \
                                i['_source']['star_atlas_info']['link_info']['title']
                    del i['_source']['star_atlas_info']
                count.append(i['_source'])

        return count

    def get_data(self, count):

        promotion_ids = set()
        for i8 in count:
            if i8.get('promotion_id'):
                promotion_ids.add(i8['promotion_id'])
        promotion_ids = list(promotion_ids)
        count4 = []
        size = 1000
        for r1 in range(len(promotion_ids) // size + 1):
            bq = {
                "size": size,
                "_source": ["promotion_id", "visitor_count"],
                "query": {
                    "bool": {
                        "must": [
                            {
                                "range": {
                                    "create_time": {
                                        "gte": "2020-06-01",
                                        "lte": "2021-12-31"
                                    }
                                }
                            },
                            {
                                "terms": {
                                    "promotion_id": promotion_ids[r1 * size: (r1 + 1) * size]
                                }
                            }
                        ]
                    }
                },
                "collapse": {
                    "field": "promotion_id"
                },
                "sort": [
                    {
                        "visitor_count": {
                            "order": "desc"
                        }
                    }
                ]
            }
            elp = ESLogShopDou.search(bq)
            for i9 in elp['hits']['hits']:
                i9['_source']['promotion_id'] = str(i9['_source']['promotion_id'])
                count4.append(i9['_source'])

        p5 = {i7['promotion_id']: i7 for i7 in count4}

        for j6 in count:
            if 'promotion_id' in j6:
                pro = p5.get(j6['promotion_id'], '')
                if 'visitor_count' in pro:
                    j6['visitor_count'] = pro['visitor_count']
                else:
                    j6['visitor_count'] = ''

        user_ids = []
        for i1 in count:
            if 'user_id' in i1:
                user_ids.append(i1['user_id'])
        user_ids = list(set(user_ids))

        count1 = []
        size = 1000
        for i in range(len(user_ids) // size + 1):
            body = {
                "size": size,
                "_source": ["user_id", "douyin_id", "nickname", "follower_num", "liked_count", "description",
                    "enterprise_verify", "verification", "birthday", "sex", "city", "video_count", "like_count",
                    "following_num", "share_info.share_url"
                ],
                "query": {
                    "terms": {
                        "user_id": user_ids[i * size: (i + 1) * size]
                    }
                }
            }
            eu = ESUserDou.search(body)
            for i3 in eu['hits']['hits']:
                i3['_source']['user_id'] = str(i3['_source']['user_id'])
                if 'sex' in i3['_source']:
                    i3['_source']['sex'] = i3['_source']['sex']
                else:
                    i3['_source']['sex'] = ''
                if 'share_info' in i3['_source']:
                    i3['_source']['share_url'] = i3['_source']['share_info']['share_url']
                count1.append(i3['_source'])
        p = {i4['user_id']: i4 for i4 in count1}
        for j1 in count:
            if 'user_id' in j1:
                user = p.get(j1['user_id'], '')
                if 'nickname' in user:
                    j1['nickname'] = user['nickname']
                else:
                    j1['nickname'] = ''
                if 'douyin_id' in user:
                    j1['douyin_id'] = user['douyin_id']
                else:
                    j1['douyin_id'] = ''
                if 'birthday' in user:
                    j1['birthday'] = int(2020 - int(str(user['birthday'])[:4]))
                else:
                    j1['birthday'] = ''
                if 'follower_num' in user:
                    j1['follower_num'] = user['follower_num']
                else:
                    j1['follower_num'] = ''
                if 'share_url' in user:
                    j1['主页链接'] = user['share_url']
                else:
                    j1['主页链接'] = ''
                if 'liked_count' in user:
                    j1['用户点赞数'] = user['liked_count']
                else:
                    j1['用户点赞数'] = ''
                if 'description' in user:
                    j1['up_description'] = user['description']
                else:
                    j1['up_description'] = ''
                if 'sex' in user:
                    j1['性别'] = user['sex']
                else:
                    j1['性别'] = ''
                if 'city' in user:
                    j1['city'] = user['city']
                else:
                    j1['city'] = ''
                if 'video_count' in user:
                    j1['视频数'] = user['video_count']
                else:
                    j1['视频数'] = ''
                if 'like_count' in user:
                    j1['用户喜欢数'] = user['like_count']
                else:
                    j1['用户喜欢数'] = ''
                if 'following_num' in user:
                    j1['关注数'] = user['following_num']
                else:
                    j1['关注数'] = ''
                if 'enterprise_verify' in user:
                    j1['企业认证'] = user['enterprise_verify']
                else:
                    j1['企业认证'] = ''
                if 'verification' in user:
                    j1['用户认证'] = user['verification']
                else:
                    j1['用户认证'] = ''

        count2 = []
        size = 1000
        for r in range(len(user_ids) // size + 1):
            bd = {
                "size": size,
                "_source": ["user_id", "is_star", "mcn_name", "short_video_price", "long_video_price",
                    "long_long_video_price", "expected_cpm", "tags", "tags_level_one", "tags_style"
                ],
                "query": {
                    "terms": {
                        "user_id": user_ids[r * size: (r + 1) * size]
                    }
                }
            }
            ess = ESStarDou.search(bd)
            for i5 in ess['hits']['hits']:
                i5['_source']['user_id'] = str(i5['_source']['user_id'])
                count2.append(i5['_source'])

        p1 = {i6['user_id']: i6 for i6 in count2}
        for j2 in count:
            if 'post_id' in j2:
                j2['post_id'] = j2['post_id']
            else:
                j2['post_id'] = ''
            if 'comment_count' in j2:
                j2['评论数'] = j2['comment_count']
            else:
                j2['评论数'] = ''
            if 'share_count' in j2:
                j2['转发数'] = j2['share_count']
            else:
                j2['转发数'] = ''
            if 'liked_count' in j2:
                j2['点赞数'] = j2['liked_count']
            else:
                j2['点赞数'] = ''
            if 'play_count' in j2:
                if j2['play_count'] == '':
                    j2['play_count'] = j2['play_count']
                elif j2['play_count'] == 0:
                    j2['play_count'] = ''
                elif j2['play_count'] > 0:
                    j2['play_count'] = j2['play_count']
            else:
                j2['play_count'] = ''
            if j2.get('duration', ''):
                if j2['duration'] >= 1000:
                    j2['duration_s'] = j2['duration'] / 1000
                elif j2['duration'] < 1000:
                    j2['duration_s'] = j2['duration']
            else:
                j2['duration_s'] = ''
            if 'post_create_time' in j2:
                j2['post_create_time'] = str(j2['post_create_time'])
            else:
                j2['post_create_time'] = ''
            if 'play_url' in j2:
                j2['play_url'] = j2['play_url']
            else:
                j2['play_url'] = ''
            if 'description' in j2:
                j2['post_description'] = j2['description']
            else:
                j2['post_description'] = ''
            if 'is_star_task' in j2:
                j2['是否是商单'] = j2['is_star_task']
            else:
                j2['是否是商单'] = ''
            if 'promotion_id' in j2:
                j2['商品ID'] = j2['promotion_id']
            else:
                j2['商品ID'] = ''
            if 'title' in j2:
                j2['title'] = j2['title']
            else:
                j2['title'] = ''
            if 'price' in j2:
                j2['price'] = j2['price']
            else:
                j2['price'] = ''
            if 'sales' in j2:
                j2['sales'] = j2['sales']
            else:
                j2['sales'] = ''
            if 'visitor_count' in j2:
                j2['visitor_count'] = j2['visitor_count']
            else:
                j2['visitor_count'] = ''
            if '商品链接' in j2:
                j2['商品链接'] = j2['商品链接']
            else:
                j2['商品链接'] = ''
            if 'ocr' in j2:
                j2['ocr'] = j2['ocr']
            else:
                j2['ocr'] = ''
            if 'play_url' in j2:
                j2['play_url'] = j2['play_url']
            else:
                j2['play_url'] = ''
            if 'star_atlas_info_web_url' in j2:
                j2['star_atlas_info_web_url'] = j2['star_atlas_info_web_url']
            else:
                j2['star_atlas_info_web_url'] = ''
            if 'star_atlas_info_title' in j2:
                j2['star_atlas_info_title'] = j2['star_atlas_info_title']
            else:
                j2['star_atlas_info_title'] = ''
            if 'anchor_info_title' in j2:
                j2['anchor_info_title'] = j2['anchor_info_title']
            else:
                j2['anchor_info_title'] = ''
            if 'user_id' in j2:
                star = p1.get(j2['user_id'])
                if star != None:
                    if 'is_star' in star:
                        j2['是否是星图用户'] = star['is_star']
                    else:
                        j2['是否是星图用户'] = ''
                    if 'short_video_price' in star:
                        j2['short_video_price'] = star['short_video_price']
                    else:
                        j2['short_video_price'] = ''
                    if 'long_video_price' in star:
                        j2['long_video_price'] = star['long_video_price']
                    else:
                        j2['long_video_price'] = ''
                    if 'long_long_video_price' in star:
                        j2['long_long_video_price'] = star['long_long_video_price']
                        if j2['long_long_video_price'] > 0:
                            j2['long_long_video_price'] = j2['long_long_video_price']
                        elif j2['long_long_video_price'] == 0:
                            j2['long_long_video_price'] = ''
                    else:
                        j2['long_long_video_price'] = ''
                    if 'expected_cpm' in star:
                        j2['预期CPM'] = star['expected_cpm']
                    else:
                        j2['预期CPM'] = ''
                    if 'tags' in star:
                        j2['tags'] = star['tags'] if len(star['tags']) else ''
                    else:
                        j2['tags'] = ''
                    if 'tags_style' in star:
                        j2['tags_style'] = star['tags_style'] if len(star['tags_style']) else ''
                    else:
                        j2['tags_style'] = ''
                    if 'tags_level_one' in star:
                        j2['tags_level_one'] = star['tags_level_one'] if len(star['tags_level_one']) else ''
                    else:
                        j2['tags_level_one'] = ''
                    if 'mcn_name' in star:
                        j2['MCN'] = star['mcn_name']
                    else:
                        j2['MCN'] = ''
                else:
                    j2['是否是星图用户'] = ""
                    j2['预期CPM'] = ""
                    j2['tags'] = ""
                    j2['tags_style'] = ""
                    j2['tags_level_one'] = ""
                    j2['MCN'] = ""

        for z in count:
            if 'short_video_price' in z:
                z['short_video_price'] = z['short_video_price']
            else:
                z['short_video_price'] = ''
            if 'long_video_price' in z:
                z['long_video_price'] = z['long_video_price']
            else:
                z['long_video_price'] = ''
            if 'long_long_video_price' in z:
                z['long_long_video_price'] = z['long_long_video_price']
            else:
                z['long_long_video_price'] = ''
            if ((z['short_video_price'] != '') or (z['long_video_price'] != '') or (
                    z['long_long_video_price'] != '')) and \
                    z['duration_s'] != '':
                if z['duration_s'] >= 0 and z['duration_s'] < 21:
                    z['end_price'] = z['short_video_price']
                elif z['long_video_price'] >= 0 and z['long_long_video_price'] == '':
                    if z['duration_s'] >= 21:
                        z['end_price'] = z['long_video_price']
                elif z['long_video_price'] >= 0 and z['long_long_video_price'] >= 0:
                    if z['duration_s'] >= 21 and z['duration_s'] < 61:
                        z['end_price'] = z['long_video_price']
                    if z['duration_s'] >= 61:
                        z['end_price'] = z['long_long_video_price']
            else:
                z['end_price'] = ''

        items = pd.DataFrame(count)
        # 删除重复项的函数 subset表示要删除的重复项指定列
        items = items.drop_duplicates(subset=['sum_match_keyword', 'post_id'], keep='first')
        print("items.columns: ", items.columns)
        items = items.to_dict(orient='records')
        print("items: ", len(items))

        return items

    def re_match(self, items, kw1_kw2_dic):
        """ kw1前后5个字以内出现kw2, 算匹配成功 """

        new_items = []

        for item in items:
            matched_items = []
            for col in ["description", "ocr"]:
                text = item[col]
                for k1, k2 in kw1_kw2_dic.items():
                    for kw in k2:
                        pattern1 = re.compile(k1 + r'[^，。,.】·：；;▪+、➕\n\t\r]{0,5}?' + kw)
                        matched_items1 = re.findall(pattern1, str(text).lower())
                        pattern2 = re.compile(kw + r'[^，。,.】·：；;▪+、➕\n\t\r]{0,5}?' + k1)
                        matched_items2 = re.findall(pattern2, str(text).lower())
                        if matched_items1:
                            matched_items.extend(matched_items1)
                        if matched_items2:
                            matched_items.extend(matched_items2)
            if len(matched_items) > 0:
                item["re_matched_item"] = matched_items[0]
                new_items.append(item)

        print("new_items: ", len(new_items))

        items = pd.DataFrame(items)
        new_items = pd.DataFrame(new_items)

        columns1 = ['MCN', 'anchor_info_title', 'birthday', 'city', 'comment_count',
                   'description', 'douyin_id', 'duration', 'duration_s', 'end_price',
                   'follower_num', 'is_star_task', 'liked_count', 'long_long_video_price',
                   'long_video_price', 'match_keyword', 'nickname', 'ocr', 'play_count',
                   'play_url', 'post_create_time', 'post_description', 'post_id', 'price',
                   'promotion_id', 'sales', 'share_count', 'short_video_price',
                   'star_atlas_info_title', 'star_atlas_info_web_url', 'sum_match_keyword',
                   'tags', 'tags_level_one', 'tags_style', 'title', 'up_description',
                   'user_id', 'visitor_count', '主页链接', '企业认证', '关注数', '商品ID', '商品链接', '性别',
                   '是否是商单', '是否是星图用户', '点赞数', '用户喜欢数', '用户点赞数', '用户认证', '视频数', '评论数',
                   '转发数', '预期CPM']
        columns2 = ['MCN', 'anchor_info_title', 'birthday', 'city', 'comment_count',
                   'description', 'douyin_id', 'duration', 'duration_s', 'end_price',
                   'follower_num', 'is_star_task', 'liked_count', 'long_long_video_price',
                   'long_video_price', 'match_keyword', 'nickname', 'ocr', 'play_count',
                   'play_url', 'post_create_time', 'post_description', 'post_id', 'price',
                   'promotion_id', 'sales', 'share_count', 'short_video_price',
                   'star_atlas_info_title', 'star_atlas_info_web_url', 'sum_match_keyword',
                   'tags', 'tags_level_one', 'tags_style', 'title', 'up_description',
                   'user_id', 'visitor_count', '主页链接', '企业认证', '关注数', '商品ID', '商品链接', '性别',
                   '是否是商单', '是否是星图用户', '点赞数', '用户喜欢数', '用户点赞数', '用户认证', '视频数', '评论数',
                   '转发数', '预期CPM', 're_matched_item']

        with pd.ExcelWriter("douyin_match_test.xlsx", engine='xlsxwriter', options={'strings_to_urls': False}) as writer:
            items.to_excel(writer, sheet_name='sheet1', index=False, columns=columns1)
            new_items.to_excel(writer, sheet_name="sheet2", index=False, columns=columns2)

    def run(self, excluded_words_string, must_words_string, start_date, end_date, kw1_kw2_dic):

        # 获取 "排除条件"
        not_clause = self.exclude_word_match(excluded_words_string)
        # 获取 "匹配条件"
        should_clause, words = self.must_word_match(must_words_string)
        # 根据 排除条件 和 匹配条件 筛选数据
        count = self.query_data("xxxxx", must_words_string, words, should_clause, [start_date, end_date], not_clause)
        items = self.get_data(count)

        self.re_match(items, kw1_kw2_dic)


if __name__ == "__main__":

    excluded_words_string = ""

    must_words_string = "lancome|兰蔻"

    start_date = "2021-08-25"
    end_date = "2021-08-26"

    kw1_kw2_dic = {
        "兰蔻": "精华",
        "粉底液": "推荐",
        "面霜": "美白"
    }

    s = SeachDataDY()

    s.run(excluded_words_string, must_words_string, start_date, end_date, kw1_kw2_dic)